package com.back.service.impl;

import com.back.entity.Movie;
import com.back.mapper.MovieDao;
import com.back.service.inte.MovieService;
import com.back.uitls.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class MovieServiceImpl implements MovieService {

    @Value("${file_save_path}")
    String fileSavePath;

    @Autowired
    MovieDao movieDao;

    /**
     * @param movie
     * @param img
     */
    @Override
    public void insertMovie(Movie movie, byte[] img, String imgName) {
        // 随机生成一个文件名（UUID）
        String filename = UUID.randomUUID() +imgName;
        FileUtil.saveFileByBytes(img, fileSavePath , filename);
        // 保存文件
        movie.setImg(fileSavePath+"/"+filename);
        // 保存电影信息
        movieDao.insertMovie(movie);
    }

    /**
     * @param movie_id
     * @return
     */
    @Override
    public Map<String, Object> selectMovieById(int movie_id) throws Exception {
        Movie movie = movieDao.selectMovieById(movie_id);
        if (movie == null) {
            throw new Exception("电影不存在");
        }
        Map<String, Object> map = getStringObjectMap(movie);

        return map;
    }

    /**
     * @param name
     * @param types
     * @param offset
     * @param pagesize
     * @return
     */
    @Override
    public List<Map<String, Object>> selectMovieByNameAndTypes(String name, List<String> types, int offset, int pagesize) {
        List<Movie> movies = movieDao.selectMovieByNameAndTypes(name, types, offset, pagesize);

        List<Map<String, Object>> list = new ArrayList<>();
        for (Movie movie : movies) {
            Map<String, Object> map = getStringObjectMap(movie);
            list.add(map);
        }
        return list;
    }

    /**
     * @param name
     * @param types
     * @return
     */
    @Override
    public int selectMovieByNameAndTypesTotal(String name, List<String> types) {
        return movieDao.selectMovieCountByNameAndTypes(name, types);
    }



    /**
     * @param limit
     * @return
     */
    @Override
    public List<Map<String, Object>> selectRandomMovies(int limit) {
        List<Movie> movies = movieDao.selectRandomMovies(limit);
        List<Map<String, Object>> list = new ArrayList<>();
        for (Movie movie : movies) {
            Map<String, Object> map = getStringObjectMap(movie);
            list.add(map);
        }
        return list;
    }

    private static Map<String, Object> getStringObjectMap(Movie movie) {
        String img_base64 = "";

        try {
            byte[] bytesByFile = FileUtil.getBytesByFile(movie.getImg());
            img_base64 = FileUtil.byte2Base64(bytesByFile);
        }catch (Exception e){
            img_base64 = "";
        }

        Map<String, Object> map = Map.of(
                "id", movie.getId(),
                "name", movie.getName(),
                "type", movie.getType(),
                "description", movie.getDescription(),
                "nation", movie.getNation(),
                "date", new SimpleDateFormat("yyyy-MM-dd").format(movie.getDate()),
                "img", img_base64,
                "page_view", movie.getPage_view(),
                "score", movie.getNum_score() == 0 ? 0 : movie.getTotal_score() / movie.getNum_score()
        );
        return map;
    }

    /**
     * @return
     */
    @Override
    public List<String> selectMovieTypes() {
        List<String> types = new ArrayList<>();
        types.add("动作");
        types.add("喜剧");
        types.add("爱情");
        types.add("科幻");
        types.add("恐怖");
        types.add("战争");
        types.add("犯罪");
        types.add("奇幻");
        types.add("冒险");
        types.add("悬疑");
        types.add("动画");
        return types;
    }

    /**
     * @param movie
     */
    @Override
    public void updateMovie(Movie movie) {
        movieDao.updateMovie(movie);
    }


}
